package com.zjeav.lingjiao.base.utils;

import android.app.Activity;
import android.os.Environment;

import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.response.VersionResponse;
import com.zjeav.lingjiao.base.view.DownloadApk;
import com.zjeav.lingjiao.base.view.UpdateAppDialog;

import java.io.File;

/**
 * 检测APP更新
 */
public class DetectionUpdate {

    public static void checkApp(Result<VersionResponse> responseResult, Activity activity, String clickType){
        if(responseResult!=null){
            String appversion= responseResult.getData().getAppversion();
            String appversioncode = responseResult.getData().getAppversioncode();
            String releaseTime = responseResult.getData().getReleaseTime();
            String desc = responseResult.getData().getDesc();
            String pkgurl = responseResult.getData().getPkgurl();
            String pkgmd5 = responseResult.getData().getPkgmd5();

            if(StringUtils.packageCode(activity)>=Integer.parseInt(appversioncode)){
                return;
            }

            if(!StringUtils.isNullOrEmpty(pkgmd5)){
                String appName = "lingo.apk";

                final DownloadApk downloadApk = new DownloadApk(activity, appName, activity);
                String path = Environment.getExternalStorageDirectory()+File.separator+appName;

                File apkFile = new File(path);

                String[] strArr = desc.split("/");
                String newDes= StringUtils.join("\n\n",0,strArr.length,strArr);
                if (apkFile.exists()){

                    String getFileMd5 = DigestUtil.getFileMD5(apkFile,"MD5");
                    if (getFileMd5.equalsIgnoreCase(pkgmd5)){
                        final UpdateAppDialog updateAppDialog = new UpdateAppDialog(activity);
                        updateAppDialog.setNewversion("更新版本已下载，\r\n是否安装?");
                        updateAppDialog.setVersioncodename(appversion)                                                                                        ;
                        updateAppDialog.setMessage(newDes);
                        updateAppDialog.setYesOnclickListener("立即安装", new UpdateAppDialog.onYesOnclickListener() {
                            @Override
                            public void onYesClick() {
                                updateAppDialog.dismiss();
                                downloadApk.update();
                            }
                        });
                        updateAppDialog.setNoOnclickListener("", new UpdateAppDialog.onNoOnclickListener() {
                            @Override
                            public void onNoClick() {
                                updateAppDialog.dismiss();
                            }
                        });
                        updateAppDialog.show();
                    }else {
                        apkFile.delete();
                        downloadApk.ShowDialog(newDes, pkgurl,appversion);
                    }
                }else {
                    downloadApk.ShowDialog(newDes, pkgurl,appversion);
                }
            }else {
                if ("mine".equals(clickType)){
                    UIUtils.showToast("该版本已是最新版本");
                }
            }

        }else {
            if ("mine".equals(clickType)){
                UIUtils.showToast("该版本已是最新版本");
            }
        }
    }

}
